package com.tsd.system.service.impl;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysGroupRoleMapper;
import com.tsd.system.entity.SysGroupRole;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysGroupRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysGroupRole 服务实现层
 *
 * @author Hillpool
 * @date 2019/03/12 12:20:51
 */
@Service
public class SysGroupRoleServiceImpl extends BaseServiceImpl implements SysGroupRoleService {

    @Resource
    private SysGroupRoleMapper sysGroupRoleMapper;

    @Override
    public SysGroupRole getSysGroupRoleById(Long id) throws Exception {
        return sysGroupRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveSysGroupRole(SysGroupRole record, SysUser sysUser) throws Exception {
        super.checkEmpty(record.getGroup_sid(), "缺少参数groupSid");
        super.checkEmpty(record.getRole_sid(), "缺少参数roleSid");
        SysGroupRole old = sysGroupRoleMapper.selectBySysGroupRole(record);
        if (old != null) {
            record.setId(old.getId());
        }
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, sysUser, TYPE_CREATE);
            sysGroupRoleMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, sysUser, TYPE_UPDATE);
            sysGroupRoleMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void deleteSysGroupRole(Long id) throws Exception {
        sysGroupRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SysGroupRole> findSysGroupRoles(Map<String, Object> params, PageBean page) throws Exception {
        return sysGroupRoleMapper.selectByPage(params, page);
    }

    @Override
    public List<SysGroupRole> findSysGroupRolesByGroupSid(String groupSid) throws Exception {
        return sysGroupRoleMapper.selectByGroupSid(groupSid);
    }

    @Override
    public void saveSysRoleList2Group(SysUser opUser, String groupSid, List<String> roleIdList) throws Exception {
        for (String roleId : roleIdList) {
            SysGroupRole groupRole = new SysGroupRole();
            groupRole.setGroup_sid(groupSid);
            groupRole.setRole_sid(roleId);
        }
    }
}
